package com.hxkcloud.patient.photo;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.utils.SharedPreferenceUtil;

import java.util.ArrayList;

public class MeenoPhotoActivity extends Activity implements OnClickListener {
	private RelativeLayout meeno_photo_back_container;
	private Button meeno_photo_confirm;
	private GridView meeno_photo_gv;
	private ArrayList<MeenoPhotoEntity> listForPhotos = new ArrayList<MeenoPhotoEntity>();
	private MeenoPhotoAdapter adapter;
	private int maxNumber;

	public static void jumpToMeenoPhotoForResult(Activity activity, int maxNumber, int requestCode) {
		Intent intent = new Intent(activity, MeenoPhotoActivity.class);
		intent.putExtra("maxNumber", maxNumber);
		activity.startActivityForResult(intent, requestCode);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_meeno_photo);
		getScreenSize();
		maxNumber = getIntent().getIntExtra("maxNumber", 9);
		initView();
	}

	private void initView() {
		meeno_photo_back_container = (RelativeLayout) findViewById(R.id.meeno_photo_back_container);
		meeno_photo_back_container.setOnClickListener(this);
		meeno_photo_confirm = (Button) findViewById(R.id.meeno_photo_confirm);
		meeno_photo_confirm.setOnClickListener(this);
		meeno_photo_gv = (GridView) findViewById(R.id.meeno_photo_gv);
		adapter = new MeenoPhotoAdapter(this, listForPhotos, meeno_photo_confirm, maxNumber);
		meeno_photo_gv.setAdapter(adapter);

		initPhotoData();
	}

	private void initPhotoData() {
		listForPhotos.clear();
		listForPhotos.addAll(AlbumHelper.getAllPhotos(this));
		adapter.notifyDataSetChanged();
		// Log.d("123123123", listForPhotos.get(0).getImagePath());
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch (viewId) {
		/**
		 * 返回
		 */
		case R.id.meeno_photo_back_container:
			finish();
			break;
		/**
		 * 确定
		 */
		case R.id.meeno_photo_confirm:
			ArrayList<String> pics = new ArrayList<String>();
			for (int i = 0; i < listForPhotos.size(); i++) {
				if (listForPhotos.get(i).isSelected()) {
					pics.add(listForPhotos.get(i).getImagePath());
				}
			}
			Intent intent = new Intent();
			intent.putExtra("pics", pics);
			setResult(RESULT_OK, intent);
			finish();
			break;
		default:
			break;
		}
	}

	protected int screenWidth;
	protected int screenHeight;

	protected void getScreenSize() {
		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			if (YjhApplication.StatusBarHeight > 0) {
				getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			} else {
				int height = SharedPreferenceUtil.getStatusBarHeight(this);
				if (height > 0) {
					getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
				}
			}
		}
	}
}
